package astri.pdcontrol.controlmode;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import astri.pdcontrol.ConnectApplication;
import astri.pdcontrol.PDControlActivity;
import astri.pdcontrol.R;
import astri.pdcontrol.client.PDControlClient;
import android.view.GestureDetector;
import android.util.Log;

public class CtrlModeActivity extends Activity implements OnTouchListener,
		OnGestureListener {
	private static final int FLING_MIN_DISTANCE = 80;
	private static final int FLING_MIN_VELOCITY = 200;
	private  ConnectApplication app =null;
	private PDControlClient client =null;
	
	GestureDetector mGestureDetector;

	public void onCreate(Bundle savedInstanceState) {
		this.getWindow().setFormat(PixelFormat.RGBA_8888);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.control_main);
		mGestureDetector = new GestureDetector(this);
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.control_layout);
		layout.setOnTouchListener(this);
		layout.setLongClickable(true);
		app = (ConnectApplication)this.getApplication();
		client = app.getclient();
		
		ImageView iv_option = (ImageView) findViewById(R.id.imageView2);
		iv_option.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(CtrlModeActivity.this, CtrlOptionActivity.class);
				startActivity(intent);
				CtrlModeActivity.this.finish();
			}

		});
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		return mGestureDetector.onTouchEvent(event);

	}

	@Override
	public boolean onDown(MotionEvent e) {
		Log.v("onDown", Float.toString(e.getX()));
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		Log.v("onFling", "----------------------------");
		Log.v("e1.getX():", Float.toString(e1.getX()));
		Log.v("e2.getX():", Float.toString(e2.getX()));
		Log.v("velocityX", Float.toString(velocityX));
		Log.v("e1.getY():", Float.toString(e1.getY()));
		Log.v("e2.getY():", Float.toString(e2.getY()));
		Log.v("velocityY", Float.toString(velocityY));
		float distance_left_X = e1.getX() - e2.getX();
		float distance_right_X = e2.getX() - e1.getX();
		float distance_down_Y = e1.getY() - e2.getY();
		float distance_up_Y = e2.getY() - e1.getY();
		if (distance_left_X > FLING_MIN_DISTANCE
				&& Math.abs(velocityX) > FLING_MIN_VELOCITY
				&& Math.abs(distance_left_X) > Math.abs(distance_down_Y)) {
			// Fling left
			Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();
		} else if (distance_right_X > FLING_MIN_DISTANCE
				&& Math.abs(velocityX) > FLING_MIN_VELOCITY
				&& Math.abs(distance_right_X) > Math.abs(distance_down_Y)) {
			// Fling right
			Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();
		} else if (distance_down_Y > FLING_MIN_DISTANCE
				&& Math.abs(velocityY) > FLING_MIN_VELOCITY
				&& Math.abs(distance_down_Y) > Math.abs(distance_right_X)) {
			// Fling Up
			Toast.makeText(this, "Fling Up", Toast.LENGTH_SHORT).show();
			  try {
				client.sendQtKey(0x010000a7);
			} catch (IOException e) {
				e.printStackTrace();
			} //PD_CONTROL_KEY_FLING_UP
		} else if (distance_up_Y > FLING_MIN_DISTANCE
				&& Math.abs(velocityY) > FLING_MIN_VELOCITY
				&& Math.abs(distance_up_Y) > Math.abs(distance_left_X)) {
			// Fling Down
			Toast.makeText(this, "Fling Down", Toast.LENGTH_SHORT).show();
		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		Log.v("onLongPress", Float.toString(e.getX()));

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float arg2,
			float arg3) {
		Log.v("onScroll:e1", Float.toString(e1.getX()));
		Log.v("onScroll:e2", Float.toString(e2.getX()));
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		Log.v("onShowPress:e", Float.toString(e.getX()));

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		Log.v("onSingleTapUp:e", Float.toString(e.getX()));
		return false;
	}

}
